package com.gistone.commis.util;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.ResourceUtils;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * Created by wangfan on 2017/9/9.
 */
public class ImageUtil {

    public static String getImageString(String filepath) throws IOException {
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(filepath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            throw e;
        } finally {
            if(in != null) in.close();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return data != null ? encoder.encode(data) : "";
    }

    public static String getImageStringWithName(String filepath,String name) throws IOException {
        InputStream imageIn = new FileInputStream(new File(filepath));
        JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
        BufferedImage image = decoder.decodeAsBufferedImage();
        Graphics g = image.getGraphics();
        // 设置颜色
//        g.setColor(Color.BLACK);
        Font font = new Font("黑体",Font.BOLD,30);
        g.setFont(font);
        // 输出文字
        g.drawString(name, 10, 10 + font.getSize());
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        JPEGImageEncoder encoder1 = JPEGCodec.createJPEGEncoder(output);
        encoder1.encode(image);
        return output.toString("8859_1");
    }


    /**
     * @param srcImgPath 源图片路径
     * @param tarImgPath 保存的图片路径
     * @param waterMarkContent 水印内容
     * @param markContentColor 水印颜色
     * @param font 水印字体
     */
    public void addWaterMark(String srcImgPath, String tarImgPath, String waterMarkContent,Color markContentColor,Font font) {

        try {
            // 读取原图片信息
            File srcImgFile = new File(srcImgPath);//得到文件
            Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
            int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
            int srcImgHeight = srcImg.getHeight(null);//获取图片的高
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            g.setColor(markContentColor); //根据图片的背景设置水印颜色
            g.setFont(font);              //设置字体

            //设置水印的坐标
            int x = 10;
            int y = 10;
            g.drawString(waterMarkContent, x, y);  //画出水印
            g.dispose();
            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
            ImageIO.write(bufImg, "jpg", outImgStream);
            System.out.println("添加水印完成");
            outImgStream.flush();
            outImgStream.close();

        } catch (Exception e) {
            // TODO: handle exception
          e.printStackTrace();
        }
    }
    public int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }
/*    public static void main(String[] args) throws  Exception {
        Font font = new Font("微软雅黑", Font.PLAIN, 35);                     //水印字体
        String srcImgPath=ResourceUtils.getFile("classpath:signImg/kezhang_h.png").getAbsolutePath();//源图片地址
        String tarImgPath="/Users/wangfan/1.jpg"; //待存储的地址
        String waterMarkContent="王凡";  //水印内容
        Color color=new Color(255,255,255,128);                              //水印图片色彩以及透明度
        new ImageUtil().addWaterMark(srcImgPath, tarImgPath, waterMarkContent, color,font);

    }*/


}
